<?php
namespace Home\Controller;
use Think\Controller;
class FeedbackController extends Controller {
	public function _initialize()
    {
        //未登录、登录超时、长时间未动
    	if (empty($_SESSION['uid']) || time() > $_SESSION['overtime'] || empty($_SESSION['overtime']) ) {

    		$this->redirect('Common/login');
    	}

        session('overtime',(time()+60*60*C('overtime')));

        
        
    }


    /**
     * [news 用户新闻列表]
     * @return [type] [description]
     */
    public function news_user(){
    	$m_news = M('News');

        $count = $m_news->count();
        $Page  = new \Think\Page($count,20);
        $show  = $Page->show();

        
        $newsList = $m_news->order('top desc,id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

		$this->assign('page',$show);
		$this->assign('newsList',$newsList);
    	$this->display();
    }


    


    /**
     * [newsDetail 新闻详情]
     * @return [type] [description]
     */
    public function newsDetail(){
    	$id = $_REQUEST['id'];

    	$m_news = M('News');

    	$where['id'] = $id;
    	$newInfo = $m_news->where($where)->find();

    	$this->assign('newInfo',$newInfo);
    	$this->display();
    }


    /**
     * [feedback_user 用户反馈信息]
     * @return [type] [description]
     */
    public function feedback_user(){
        $uid =$_SESSION['uid'];
        $m_user = M('User');
        $m_feedback = M('Feedback');

        $where_user['id'] = $uid;
        $userInfo = $m_user->where($where_user)->find();

    	$where['user_id'] = $uid;

    	$count = $m_feedback->where($where)->count();
		$Page  = new \Think\Page($count,20);
		$show  = $Page->show();

    	$feedbackList = $m_feedback->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

		$this->assign('page',$show);
		$this->assign('feedbackList',$feedbackList);
        $this->assign('userInfo',$userInfo);
    	$this->display();
    }


    /**
     * [ctrl_Feedback 添加反馈留言]
     * @return [type] [description]
     */
    public function ctrl_Feedback(){
    	if (IS_AJAX) {
    		// print_r($_REQUEST);die();
    		$uid = $_REQUEST['userId'];
    		$title = trim($_REQUEST['title']);
    		$imgurl_arr =$_REQUEST['imgurl_arr'];
    		$content = trim($_REQUEST['content']);

    		$m_picture = M('Picture');
    		$m_feedback = M('Feedback');

            if (empty($title) || empty($content) || empty($uid)) {
                $this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '标题和内容不能为空'
                ));
            }else{


        		if (!empty($imgurl_arr)) {
        			// $add_pic['url'] = $img_url;
        			// $res_pic = $m_picture->add($add_pic);
        			// if ($res_pic) {
        			// 	$add_feedback['pic_id'] = $res_pic;
        			// }
                    $img_url = implode(',', $imgurl_arr);   
                    $add_feedback['pic_url'] = $img_url;
        		}

        		$add_feedback['user_id'] = $uid;
        		$add_feedback['title'] = $title;
        		$add_feedback['content'] = $content;

        		$add_feedback['createtime'] = time();

        		$res = $m_feedback->add($add_feedback);
        		if ($res) {
        			$this->ajaxReturn(array(
                        'code'=>1,
                        'msg' => '提交成功',
                        'data'=>$res,
                        'time'=>date("Y-m-d H:i:s",$add_feedback['createtime'])
                    ));
        		}else{
        			$this->ajaxReturn(array(
                        'code'=>0,
                        'msg' => '提交失败'
                    ));
        		}
    		}

    	}else{
    		$this->redirect('Common/BadRequest');
    	}
    }


    /**
     * [ctrl_deleteFeedback 删除留言]
     * @return [type] [description]
     */
    public function ctrl_deleteFeedback(){
        if (IS_AJAX) {
            $id = $_REQUEST['id'];
            $m_feedback = M('Feedback');

            $where['id'] = $id;
            $feedbackInfo = $m_feedback->where($where)->find();
            if (empty($feedbackInfo['id'])) {
                $this->ajaxReturn(array(
                    'code'=>1,
                    'msg' => '已删除，请勿重复操作'
                ));
            }else{
                $res = $m_feedback->where($where)->delete();
                if ($res) {
                    $this->ajaxReturn(array(
                        'code'=>1,
                        'msg' => '操作成功'
                    ));
                }else{
                    $this->ajaxReturn(array(
                        'code'=>0,
                        'msg' => '操作失败'
                    ));
                }
            }
        }else{
            $this->redirect('Common/BadRequest');
        }
    }

}